package com.blue.firecat.contants;

import com.blue.firecat.dto.TradeStatusDto;
import com.blue.firecat.entity.Game;
import com.blue.firecat.entity.User;
import com.blue.firecat.enums.ErrorEnum;
import com.blue.firecat.enums.UserStatusEnum;
import com.blue.firecat.enums.UserTypeEnum;
import com.blue.firecat.utils.property.PropertiesUtil;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;

import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * @描述 全局常量
 * @时间 2017/6/22 23:19
 * @作者 肖亦鸣
 */
public class Contants {
    //不需要登录拦截的请求
    public static final String[] notInterceptorUrl = {"resources", "login", "validateCode",
            "user/index.html","upload", "firecat/user/index.html", "v/v", "v/getOrder", "register", "forgetPassword"};
    //验证码
    public static final Map<String, String> LOGIN_CODE = Maps.newHashMap();
    //忘记密码  修改密码的验证码
    public static final Map<String, String> CHANGE_PASSWORD_CODE = Maps.newHashMap();
    //初始化开始时间
    public static final String DATE_MIN = "1900-01-01 00:00:00";
    //初始化结束时间
    public static final String DATE_MAX = "2099-12-31 23:59:59";

    //显示前台的信息
    public static final String MESSAGE = "msg";
    //用户的session
    public static final String USER_SESSION = "userSession";
    //返回给界面的对象
    public static final String OBJECT = "object";
    //游戏
    public static final String GAMES = "games";
    //订单
    public static final String ORDERS = "orders";
    //交易
    public static final String TRADES = "trades";
    //用户
    public static final String USERS = "users";
    //金额
    public static final String MONEY = "money";
    //交易统计
    public static final String TRADE_COUNTS = "TRADE_COUNTS";
    //订单统计
    public static final String ORDER_COUNTS = "ORDER_COUNTS";


    //游戏缓存
    public static final Result<List<Game>> GAME_RESULT = new Result<>();
    //交易游戏缓存,就是没有数量的
    public static final Result<List<Game>> TRADE_GAME_RESULT = new Result<>();
    //交易状态缓存
    public static final List<TradeStatusDto> TRADE_STATUS_RESULT = Lists.newArrayList();
    //游戏可接受计数
    public static final Map<Integer, Integer> GAME_COUNT_MAP = new ConcurrentHashMap<>();
    //订单类型的数量
    public static final Map<String, Integer> ORDER_COUNT_MAP = new ConcurrentHashMap<>();


    //图片上传路径
    public static final String PIC_PATH = PropertiesUtil.getProperty("picPath");
    //图片格式
    public static final String PIC_SUFFIX = PropertiesUtil.getProperty("picSuffix");
    //切割符
    public static final String PIC_CUTTING = PropertiesUtil.getProperty("picCutting");
    //文件大小
    public static final Long PIC_SIZE = Long.valueOf(PropertiesUtil.getProperty("picSize")) * 1024 * 1024;
    //文件数量
    public static final int PIC_MAX_NUM = Integer.valueOf(PropertiesUtil.getProperty("picMaxNum"));

    //文件名
    public static final String FILE_NAME = PropertiesUtil.getProperty("excelFileName");
    //订单表头
    public static final String[] ORDER_EXPORT_HEADER = {"订单编号", "创建人id", "创建人姓名","下单来源", "游戏名称", "区服", "代练内容", "验收时间", "订单类型", "备注", "订单价", "结算价"};
    //交易表头
    public static final String[] TRADE_EXPORT_HEADER = {"交易id", "订单编号", "打手id", "打手姓名", "游戏名称", "区服", "代练内容", "验收时间", "状态", "备注", "结算价"};


    //定时任务时间
    public static final Integer HOUR = Integer.valueOf(PropertiesUtil.getProperty("hour"));


    /**
     * 判断用户没有激活返回true 不是管理员返回true
     *
     * @param user
     * @param result
     * @return
     */
    public static boolean verifyAuthority(User user, Result result) {
        if (isActivity(user, result)) return true;
        if (!user.getType().equals(UserTypeEnum.ADMIN.getCode())) {
            result.setCode(ErrorEnum.USER_UNABLE_TO_DO);
            return true;
        }
        return false;
    }

    /**
     * 判断用户是不是管理员 或客服 和 是不是激活状态
     *
     * @param user
     * @param result
     * @return
     */
    public static boolean verifyAuthorityOrCustom(User user, Result result) {
        if (isActivity(user, result)) return true;
        if (user.getType().equals(UserTypeEnum.BEATER.getCode())) {
            result.setCode(ErrorEnum.USER_UNABLE_TO_DO);
            return true;
        }
        return false;
    }

    private static boolean isActivity(User user, Result result) {
        if (user == null || user.getStatus().equals(UserStatusEnum.TERMINATED.getCode())) {
            result.setCode(ErrorEnum.USER_IS_NOT_ENABLED);
            return true;
        }
        return false;
    }

    /**
     * @param user
     * @return
     */
    public static boolean isBeater(User user) {
        if (user == null || user.getType().equals(UserTypeEnum.BEATER.getCode())) {
            return true;
        }
        return false;
    }

    /**
     * 通过map获取数量
     *
     * @param map
     * @return
     */
    public static Object getNumberByMap(Map<String, Integer> map) {
        int numberSum = 0;
        if (map == null) {
            return numberSum;
        }
        for (Integer number : map.values()) {
            numberSum = numberSum + number;
        }
        return numberSum;
    }

}
